<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.real_name" placeholder="客户姓名" style="width: 130px;margin-top:8px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.shareholder" placeholder="股东" class="filter-item" style="width:130px;margin-top:8px">
        <el-option label="股东" value="" />
        <el-option
          v-for="item in shareholderList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select v-model="listQuery.c_id" placeholder="选择门店" filterable class="filter-item" style="width:150px;margin-top:8px">
        <el-option label="选择门店" value="" />
        <el-option
          v-for="item in company_list"
          :key="item.id"
          :label="item.c_name"
          :value="item.id"
        />
      </el-select>
      <onwerCompany ref="owner_company" />
      <el-date-picker v-model="listQuery.date" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="开始日期" />
      <el-date-picker v-model="listQuery.date_end" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="结束日期" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:8px" @click="handleFilter">查询</el-button>
      <!-- <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="excelout" style="margin-top:8px" >导出</el-button> -->
      <excelout />

    </div>
    <el-row style="margin-top:20px;font-size:14px;color:#666">
      <el-col :span="4">总投入金额：{{ count.all_benjin }}</el-col>
      <el-col :span="4">已分配总金额：{{ count.all_settle_money }}</el-col>
      <el-col :span="4">已分配总本金：{{ count.settle_benjin }}</el-col>
      <el-col :span="4">已分配总分利：{{ count.all_benefits }}</el-col>
      <el-col :span="4">手续费：{{ count.all_sxf }}</el-col>
    </el-row>
    <el-table :key="tableKey" v-loading="listLoading" :element-loading-text="loadingtext" :data="list" border fit highlight-current-row style="width: 100%;margin-top: 20px;">
      <el-table-column label="股东姓名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分配日期" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.settle_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.real_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单日期" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单融资金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.total_price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="股东本金" align="center">
        <template slot-scope="{row}">
          <span>{{ row.money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对应期数" align="center">
        <template slot-scope="{row}">
          <span>{{ row.num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付日期" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.time_date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本期租金" align="center">
        <template slot-scope="{row}">
          <span>{{ row.total_money }}</span>
        </template>
      </el-table-column>

      <el-table-column label="应付本金" align="center">
        <template slot-scope="{row}">
          <span>{{ row.i_benjin }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付租金收益" align="center">
        <template slot-scope="{row}">
          <span>{{ row.money_rate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付流量费" align="center">
        <template slot-scope="{row}">
          <span>{{ row.flow }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实付租金" align="center">
        <template slot-scope="{row}">
          <span>{{ row.paid_money }}</span>
        </template>
      </el-table-column>

      <el-table-column label="股东本期本金" align="center">
        <template slot-scope="{row}">
          <span>{{ row.benjin }}</span>
        </template>
      </el-table-column>

      <el-table-column label="股东本期分利" align="center">
        <template slot-scope="{row}">
          <span>{{ row.benefits }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手续费" align="center">
        <template slot-scope="{row}">
          <span>{{ row.sxf }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门店" align="center">
        <template slot-scope="{row}">
          <span>{{ row.c_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="出资公司" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{ row.company_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center">
        <template slot-scope="{row}">
          <span>{{ row.desc }}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import excelout from '@/components/excelout/index.vue'
import {
  getSettleList,
  getShareHolder,
  exceloutday, exceloutsettle
} from '@/api/finance'
import waves from '@/directive/waves' // waves directive
import {
  formatDate
} from '@/utils/formatDate.js'
import {
  getAllList
} from '@/api/user'
import {
  parseTime, getNodes
} from '@/utils'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import onwerCompany from '@/components/company/owner_company.vue'
import { merchantListOnlyMerchant } from '@/api/merchant'
export default {
  name: 'ComplexTable',
  components: {
    Pagination, excelout, onwerCompany
  },
  directives: {
    waves
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: '禁用',
        1: '启用'
      }
      return statusMap[status]
    },

    formatDate(time) {
      time = time * 1000
      const date = new Date(time)

      return formatDate(date, 'yyyy-MM-dd')
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      company_list: [],
      settleAble: false,
      shareholderList: {},
      nodes: [],
      settle_form: {

      },
      count: [],
      value1: '',
      value2: '',
      tableKey: 0,
      list: null,
      payway: [],
      collector: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        shareholder: null,
        real_name: null,
        date: null,
        date_end: ''
      },
      shareQuery: {
        limit: 100
      },
      dialogFormVisible: false,
      dialogStatus: '',
      listMerchantQuery: {
        page: 1,
        limit: 500
      }
    }
  },
  created() {
    this.getShareHolder()
    this.cacheQueryFormat()
    this.getCompanyList()
    this.getList()
    this.nodes = getNodes()
    // 添加钩子，页面刷新
    window.addEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  mdestroyed() {
    // 添加钩子，页面刷新
    window.removeEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  methods: {
    beforeunloadFn(e) {
      // 刷新页面事件
      sessionStorage.removeItem('settle')
    },
    cacheQueryFormat() {
      const cacheQuery = sessionStorage.getItem('settle')
      if (cacheQuery) {
        this.listQuery = JSON.parse(cacheQuery)
      }
    },
    getCompanyList() {
      merchantListOnlyMerchant(this.listMerchantQuery).then(response => {
        this.company_list = response.data.company
        this.sale_list = response.data.sale.data
      })
    },
    excelout() {
      this.listLoading = true
      this.loadingtext = '正在生成表格，请稍等...'
      this.listQuery.page = 1
      this.listQuery.limit = this.total
      exceloutsettle(this.listQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },
    getShareHolder() {
      getShareHolder(this.shareQuery).then(response => {
        this.shareholderList = response.data.data
      })
    },

    getList() {
      sessionStorage.setItem('settle', JSON.stringify(this.listQuery))
      this.listLoading = true
      getSettleList(this.listQuery).then(response => {
        this.list = response.data.data
        this.count = response.data.count[0]
        this.total = response.data.total
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.2 * 1000)
      })
    },

    handleFilter() {
      const owner_company_id = this.$refs.owner_company.$data.listQuery.company_id
      this.listQuery.owner_company_id = owner_company_id
      this.getList()
    }
  }
}
</script>
